﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Bean;
using BarCode.Common;
using System.Data;
using BarCode.Module;
using System.Data.OleDb;

namespace BarCode.AccessDao
{
    class TmpPackMdbDao
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(TmpPackMdbDao));

        private AccessHelper tmpPackMdb;
        public TmpPackMdbDao()
        {
            tmpPackMdb = new AccessHelper(GlobalVal.ipAddressInfo.DISK_VALUE + Consts.MDB_TMPPACK);
        }

        public void Release()
        {
            tmpPackMdb.Release();
        }

        /// <summary>
        /// 查询获得当前封箱条码起始编号
        /// </summary>
        /// <returns></returns>
        public string getStartPackBarcodeId(string range_PackStart, string range_PackEnd)
        {

            string packBarcodeId = range_PackStart; 
            OleDbParameter[] sqlParams = new OleDbParameter[2];
            sqlParams[0] = new OleDbParameter("range_PackStart", range_PackStart);
            sqlParams[1] = new OleDbParameter("range_PackEnd", range_PackEnd);
            DataSet retSet = tmpPackMdb.Query("select * from pack_code where pack_IDstart=@range_PackStart and pack_IDend=@range_PackEnd", sqlParams);
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    packBarcodeId = Convert.ToString(Convert.ToInt64(dt.Rows[0][2]) + 1);
                }
            }

            return packBarcodeId;
        }

        public bool savePackInfo(string range_PackStart, string range_PackEnd, string packId)
        {
            try
            {
                OleDbParameter[] sqlParams = new OleDbParameter[2];
                sqlParams[0] = new OleDbParameter("range_PackStart", range_PackStart);
                sqlParams[1] = new OleDbParameter("range_PackEnd", range_PackEnd);
                DataSet retSet = tmpPackMdb.Query("select * from pack_code where pack_IDstart=@range_PackStart and pack_IDend=@range_PackEnd", sqlParams);
                if (retSet != null && retSet.Tables.Count > 0 && retSet.Tables[0].Rows.Count > 0)
                {
                    string updSql = "update [pack_code] set [packID_now]=@packID_now where [pack_IDstart]=@range_PackStart and [pack_IDend]=@range_PackEnd ";
                    OleDbParameter[] updParams = new OleDbParameter[3];
                    updParams[0] = new OleDbParameter("packID_now", packId);
                    updParams[1] = new OleDbParameter("range_PackStart", range_PackStart);
                    updParams[2] = new OleDbParameter("range_PackEnd", range_PackEnd);
                    int updCnt = tmpPackMdb.ExecuteSql(updSql, updParams);
                }
                else
                {
                    string insSql = "INSERT INTO [pack_code] ([pack_IDstart],[pack_IDend],[packID_now]) VALUES (@pack_IDstart,@pack_IDend,@packID_now)";
                    OleDbParameter[] insParams = new OleDbParameter[3];
                    insParams[0] = new OleDbParameter("pack_IDstart", range_PackStart);
                    insParams[1] = new OleDbParameter("pack_IDend", range_PackEnd);
                    insParams[2] = new OleDbParameter("packID_now", packId);
                    int insCnt = tmpPackMdb.ExecuteSql(insSql, insParams);
                }
                return true;
            }
            catch (Exception e)
            {
                LogUtil.Error(e);
                return false;
            }
        }
    }
}
